//
//  WPPhoneViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 HuaYiSoftware. All rights reserved.
//

#import "WPPhoneViewController.h"

@interface WPPhoneViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel * introLabel;/**< 说明文本 */
@property (nonatomic, strong) UITextField * phoneTextField;/**< 账号输入框 */
@property (nonatomic, strong) UIView * codeBackView;/**< 验证码背景 */
@property (nonatomic, strong) UITextField * codeTextField;/**< 验证码输入框 */
@property (nonatomic, strong) UIButton * getCodeBtn;/**< 获取验证码按钮 */
@property (nonatomic, strong) UIButton * nextBtn;/**< 下一步按钮 */
@property (nonatomic, strong) NSTimer * timer;/**< 计时器 */
@property (nonatomic, assign) NSInteger timeCount;/**< 倒计时 */
@end

@implementation WPPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.timeCount = 60;
}
- (void)initializeUserInterface {
    self.title = @"手机号码更改";
    [self.view addSubview:self.introLabel];
    [self.view addSubview:self.phoneTextField];
    [self.view addSubview:self.codeBackView];
    [self.view addSubview:self.codeTextField];
    [self.view addSubview:self.getCodeBtn];
    [self.view addSubview:self.nextBtn];
}
#pragma mark - responds events
- (void)respondToGetCodeBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    if (_phoneTextField.text.length == 0 || ![ConfirmMobileNumber isMobileNumber:_phoneTextField.text]) {
        [self initializeAlertControllerWithMessage:@"请输入正确格式的手机号码"];
        return;
    }
    [sender setTitle:@"60s再次获取" forState:UIControlStateNormal];
    sender.userInteractionEnabled = NO;
    [self.timer setFireDate:[NSDate distantPast]];
    self.timeCount = 60;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.color = [UIColor grayColor];
    hud.labelText = @"请稍等";
    hud.detailsLabelText = @"正在发送验证码...";
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hide:YES];
    });
}
- (void)respondsToTimer {
    self.timeCount--;
    NSString * timeStr = [NSString stringWithFormat:@"%lds再次获取", (long)_timeCount];
    [_getCodeBtn setTitle:timeStr forState:UIControlStateNormal];
    if (_timeCount == 0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeBtn.userInteractionEnabled = YES;
    }
}
- (void)respondsToNextBtn {
    [self pushToNextController:[[NSClassFromString(@"WPchangePhoneViewController") alloc]init] withVCTitle:@"手机号更改"];
}
#pragma mark - 刷新数据
- (void)refreshData {
    
}
#pragma mark - getter
- (UILabel *)introLabel {
    if (!_introLabel) {
        _introLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), navBar_Fheight, 300*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            label.text = @"请在下面输入您当前的手机号码:";
            label;
        });
    }
    return _introLabel;
}

- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.introLabel.frame), SCREEN_SIZE.width, 40*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 70*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            lab.text = @"账号";
            [leftView addSubview:lab];
            text.leftView = leftView;
            text.keyboardType = UIKeyboardTypeNumberPad;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.backgroundColor = [UIColor whiteColor];
            text.placeholder = @"请输入您当前的手机号码";
            text;
        });
    }
    return _phoneTextField;
}
- (UIView *)codeBackView {
    if (!_codeBackView) {
        _codeBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.phoneTextField.frame)+10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 40*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            view;
        });
    }
    return _codeBackView;
}
- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(self.codeBackView.frame), 240*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 70*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            lab.text = @"验证码";
            [leftView addSubview:lab];
            text.leftView = leftView;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.backgroundColor = [UIColor whiteColor];
            text.placeholder = @"请输入短信验证码";
            text;
        });
    }
    return _codeTextField;
}
- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(240*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.codeTextField.frame)+5*DHFlexibleHorizontalMutiplier(), 70*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(221, 221, 221, 1);
            btn.titleLabel.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            [btn setTitleColor:COLOR_RGB(116, 116, 116, 1) forState:UIControlStateNormal];
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToGetCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _getCodeBtn;
}
- (UIButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.codeBackView.frame) + 40*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"下一步" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToNextBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _nextBtn;
}
@end
